package com.haruhinav.service.impl;

import com.haruhinav.Utils.JWTUtils;
import com.haruhinav.entity.User;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.mapper.UserMapper;
import com.haruhinav.service.UserService;
import com.haruhinav.Utils.TimeTool;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {


    @Resource
    private UserMapper userMapper;
    @Autowired
    private TimeTool timeTool;

    @Override
    public List<User> getAllUser(User user) {
        return userMapper.getAllUser( user);
    }

    @Override
    public User loginFindUser(String mid, String pwd) {
        User user =userMapper.loginFindUser(mid,
                DigestUtils.md5DigestAsHex(pwd.getBytes()));
        if(user!=null)
            user.setPassword("");
        return user;
    }

    @Override
    public Map<String,Object> findUserById(int uid) {
        User user = userMapper.findUserById(uid);
        Map<String,Object> umap = new HashMap<>();
        if(user!=null){
            user.setCreateTime(timeTool.SubTime(user.getCreateTime()));
            user.setPassword("*");
            umap.put("user",user);
            umap.put("code",0);
        }else{
            umap.put("code",-1);
        }


        return umap;
    }

    @Override
    public User findUserByMid(String mid) {
        User user = userMapper.findUserByMid(mid);
        user.setPassword("*");
        return user;
    }

    @Override
    public ResultVO insertUser(User user) {

        ResultVO result = new ResultVO();


        if(userMapper.findUserByMid(user.getMid())!=null){
//            预注册的mid已经存在
            result.setCode(-1);
            result.setType("warning");
            result.setMsg("该账号已经被使用了哦~");
        }else{
            Map<String,Object> newUserMap = new HashMap<>();
            Map<String,String> tokenMap = new HashMap<>();
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));//加密密码
            if(userMapper.insertUser(user)>0 && user.getId()!=0){//执行添加
                //User newUser = userMapper.findUserById(user.getId());

                // 创建token的参数
                tokenMap.put("userid",user.getId().toString());
                tokenMap.put("username",user.getMid());

                String token = JWTUtils.createToken(tokenMap);
                newUserMap.put("token",token);
                newUserMap.put("user",user);
                result.setCode(0);
                result.setMsg("注册成功！感谢你的加入~");
                result.setData(newUserMap);
            }else{
                result.setCode(-2);
                result.setType("error");
                result.setMsg("发生错误，注册失败！");
            }
        }
        return result;
    }

    @Override
    public ResultVO updateUser(User user) {

        ResultVO result = new ResultVO();
        if(userMapper.updateUser(user)>0){
            result.setData(user);
            result.setMsg("修改成功");
            return result;
        }
        result.setCode(-1);
        result.setMsg("修改失败");
        result.setType("error");
        result.setData(user);

        return result;
    }

    @Override
    public Map<String, String> checkUserPassword(int id, String password) {
        Map<String ,String> checkMap = new HashMap<>();

        if( userMapper.findUserById(id).getPassword()
                .equals(DigestUtils.md5DigestAsHex(password.getBytes()))){

            checkMap.put("code","0");
            checkMap.put("msg","ok");
            return checkMap;
        }
        checkMap.put("code","1");
        checkMap.put("msg","no");
        return checkMap;
    }

    @Override
    public Map<String, String> updateUserPswd(int id, String password) {
        Map<String ,String> updPwdMap = new HashMap<>();
//        新密码的MD5值
        String md5Pswd = DigestUtils.md5DigestAsHex(password.getBytes());

        if(userMapper.findUserById(id).getPassword().equals(md5Pswd)){
//            新密码与旧密码相同
            updPwdMap.put("code","1");
            updPwdMap.put("msg","与旧密码相同");
            return updPwdMap;
        }
        if(userMapper.updateUserPswd(id,md5Pswd)>0){
            updPwdMap.put("code","0");
            updPwdMap.put("msg","ok");
            return updPwdMap;
        }
        updPwdMap.put("code","2");
        updPwdMap.put("msg","error");
        return updPwdMap;
    }

    @Override
    public ResultVO updateUserFace(String faceUrl, int uid) {
        ResultVO result = new ResultVO();
        if(userMapper.updateUserFace(faceUrl,uid)>0){
            result.setCode(0);
            result.setMsg("修改成功");
            result.setData(faceUrl);
            return result;
        }
        result.setCode(-1);
        result.setType("error");
        result.setMsg("修改失败");
        return result;
    }

    @Override
    public ResultVO findUserByIdNoPswd(int uid) {
        ResultVO result = new ResultVO();
        User u =userMapper.findUserByIdNoPswd(uid);
        result.setData(u);
        return result;
    }
}
